<?php
declare (strict_types = 1);

namespace app\member\controller;
use think\Request;
class Comment extends MemberBase
{
    protected function initialize()
    {
        parent::initialize();
        $this->model = new \app\common\model\Comment;
        $this->validate = new \app\common\validate\Comment;
    }
    // 获取评论数据
    public function index(){
        $where = [];
        $path = $this->request->param('path', null);
        $page_size = $this->request->param('page_size/d',$this->page_size);
        if(empty($path)){
            return json('error',500);
        } else{
            $where['path'] = $path;
            $result = $this->model->where($where)->order('create_time desc')->paginate($page_size);
            return json($result);
        }
    }
    // 添加评论
    public function save(Request $request){
        $data = $request->post();
        $data['member_id'] = $this->request->uid || 1;
        if(!$this->validate->scene('create')->check($data)){
            return json($this->validate->getError(),500);
        }else {
            // $this->model->strict(false)->insert($data);
            $this->model->save($data);
            return json('评论发表成功！');
        }
    }
    public function likes($id){
        $this->model->where('id', $id)->inc('likes')->update();
        return json('ok');
    }
}
